package com.ruoyi.web.controller.ycdj;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.TOrganization;
import com.ruoyi.system.domain.TTeamperson;
import com.ruoyi.system.domain.TTransyear;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.TOrganizationService;
import com.ruoyi.system.service.TTeampersonService;
import com.ruoyi.system.service.TTransyearService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "组织接口")
@RestController
@RequestMapping("org")
public class OrganizationController extends BaseController {

    @Autowired
    public TOrganizationService tOrganizationService;

    @Autowired
    public TTransyearService tTransyearService;

    @Autowired
    public TTeampersonService tTeampersonService;

    @Autowired
    public ISysDeptService iSysDeptService;

    //列表
    @ApiOperation("组织列表")
    @GetMapping("getlist")
    public AjaxResult getlist(@RequestParam(value="pageNum",defaultValue = "")Integer pageNum,
                              @RequestParam(value="pageSize",defaultValue = "")Integer pageSize,
                              @RequestParam(value="status",defaultValue = "")Integer status){

        QueryWrapper<TOrganization>tOrganizationQueryWrapper=new QueryWrapper<>();
        if(status!=null){
            tOrganizationQueryWrapper.eq("status",status);
        }

        if(pageNum!=null){
            PageHelper.startPage(pageNum,pageSize);
            List<TOrganization>tOrganizationList=tOrganizationService.list();
            PageInfo<TOrganization>pageInfo=new PageInfo<>(tOrganizationList);
            return AjaxResult.success(pageInfo);
        }else{
            List<TOrganization>tOrganizationList=tOrganizationService.list();
            return AjaxResult.success(tOrganizationList);
        }
    }

    //按上下级后去组织列表
    @ApiOperation("组织树")
    @GetMapping("parentlist")
    public AjaxResult parentlist(){
        //获取最顶层结构
        List<TOrganization>list=new ArrayList<>();
        getchild(0,list);
        return AjaxResult.success(list);

    }

    //递归获取下一级
    public void getchild(Integer pid,List<TOrganization>childlist){
        QueryWrapper<TOrganization>tOrganizationQueryWrapper=new QueryWrapper<>();
        tOrganizationQueryWrapper.eq("pid",pid);
        List<TOrganization>tOrganizationList=tOrganizationService.list(tOrganizationQueryWrapper);
        if(!tOrganizationList.isEmpty()){
            for(TOrganization tOrganization:tOrganizationList){
                childlist.add(tOrganization);
                tOrganization.setChildlist(new ArrayList<>());
                getchild(tOrganization.getId(),tOrganization.getChildlist());
            }
        }
    }

    //新增
    @ApiOperation("新增组织")
    @PostMapping("add")
    public AjaxResult add(@RequestBody TOrganization tOrganization){
        //检查重复
        QueryWrapper<TOrganization>tOrganizationQueryWrapper=new QueryWrapper<>();
        tOrganizationQueryWrapper.eq("name",tOrganization.getName());
        if(tOrganizationService.getOne(tOrganizationQueryWrapper)!=null){
            return AjaxResult.warn("组织已存在");
        }
        if(addsysdept(tOrganization)>0) {
            return toAjax(tOrganizationService.save(tOrganization));
        }else{
            return AjaxResult.error("新增失败");
        }
    }

    //同步到系统部门
    public Integer addsysdept(TOrganization tOrganization){
        SysDept sysDept=new SysDept();
        sysDept.setDeptName(tOrganization.getName());
        //获取系统的上级

        sysDept.setParentId(Long.parseLong(tOrganizationService.getById(tOrganization.getPid()).getSysdeptid().toString()));
        iSysDeptService.insertDept(sysDept);
        return Integer.parseInt(sysDept.getDeptId().toString()) ;
    }

    //编辑
    @ApiOperation("编辑组织")
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TOrganization tOrganization){
        QueryWrapper<TOrganization>tOrganizationQueryWrapper=new QueryWrapper<>();
        tOrganizationQueryWrapper.eq("name",tOrganization.getName());
        tOrganizationQueryWrapper.ne("id",tOrganization.getId());
        if(tOrganizationService.getOne(tOrganizationQueryWrapper)!=null){
            return AjaxResult.warn("组织已存在");
        }
        if(editsysdept(tOrganization)>0) {
            return toAjax(tOrganizationService.saveOrUpdate(tOrganization));
        }else{
            return AjaxResult.error("编辑失败");
        }
    }

    public Integer editsysdept(TOrganization tOrganization){
        SysDept sysDept=iSysDeptService.selectDeptById(Long.parseLong(tOrganization.getSysdeptid().toString()));
        sysDept.setDeptName(tOrganization.getName());
        return iSysDeptService.updateDept(sysDept);
    }

    //删除
    @ApiOperation("删除组织")
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        TOrganization tOrganization=tOrganizationService.getById(id);
        tOrganization.setStatus(0);
        return toAjax(tOrganizationService.saveOrUpdate(tOrganization));
    }

    //启用
    @ApiOperation("启用组织")
    @GetMapping("open")
    public AjaxResult open(Integer id){
        TOrganization tOrganization=tOrganizationService.getById(id);
        tOrganization.setStatus(1);
        return toAjax(tOrganizationService.saveOrUpdate(tOrganization));
    }

    //换届
    @ApiOperation("换届列表")
    @GetMapping("getyearlist")
    public AjaxResult getyearlist(Integer orgid){
        QueryWrapper<TTransyear>tTransyearQueryWrapper=new QueryWrapper<>();
        tTransyearQueryWrapper.eq("orgid",orgid);
        tTransyearQueryWrapper.orderByDesc("year");
        tTransyearQueryWrapper.eq("status",1);
        List<TTransyear>tTransyearList=tTransyearService.list(tTransyearQueryWrapper);
        return AjaxResult.success(tTransyearList);
    }

    //新增换届
    @ApiOperation("新增换届")
    @PostMapping("addtransyear")
    public AjaxResult addtransyear(@RequestBody TTransyear tTransyear){
        //判断是否已经存在届数
        QueryWrapper<TTransyear>tTransyearQueryWrapper=new QueryWrapper<>();
        tTransyearQueryWrapper.eq("orgid",tTransyear.getOrgid());
        tTransyearQueryWrapper.eq("year",tTransyear.getYear());
        List<TTransyear>tTransyearList=tTransyearService.list(tTransyearQueryWrapper);
        if(!tTransyearList.isEmpty()){
            return AjaxResult.warn("该届已经存在或废弃，请填写其他届数");
        }
        return toAjax(tTransyearService.save(tTransyear));
    }

    //删除换届
    @ApiOperation("删除换届")
    @GetMapping("deletetransyear")
    public AjaxResult deletetransyear(Integer id){
        TTransyear tTransyear=tTransyearService.getById(id);
        tTransyear.setStatus(0);
        return toAjax(tTransyearService.saveOrUpdate(tTransyear));
    }



    //班子成员

    //列表
    @ApiOperation("班子成员列表")
    @GetMapping("getteamlist")
    public AjaxResult getteamlist(Integer orgid,Integer year){
        QueryWrapper<TTeamperson>tTeampersonQueryWrapper=new QueryWrapper<>();
        tTeampersonQueryWrapper.eq("orgid",orgid);
        tTeampersonQueryWrapper.eq("year",year);
        tTeampersonQueryWrapper.eq("status",1);
        List<TTeamperson>tTeampersonList=tTeampersonService.list(tTeampersonQueryWrapper);
        return AjaxResult.success(tTeampersonList);
    }

    //新增班子成员
    @ApiOperation("新增班子成员")
    @PostMapping("addteamperson")
    public AjaxResult addteamperson(@RequestBody TTeamperson tTeamperson){
        return toAjax(tTeampersonService.save(tTeamperson));
    }


    //编辑班子成员
    @ApiOperation("编辑班子成员")
    @PostMapping("editteamperson")
    public AjaxResult editteamperson(@RequestBody TTeamperson tTeamperson){
        return toAjax(tTeampersonService.saveOrUpdate(tTeamperson));
    }

    //删除班子成员
    @ApiOperation("删除班子成员")
    @GetMapping("deleteteamperson")
    public AjaxResult deleteteamperson(Integer id){
        TTeamperson tTeamperson=tTeampersonService.getById(id);
        tTeamperson.setStatus(0);
        return toAjax(tTeampersonService.saveOrUpdate(tTeamperson));
    }


}
